 <!-- 商家销量统计的柱状图 -->
<template>
  <div class="com-container">
    <div class="con-chart" ref="seller_ref"></div>
  </div>
</template>

<script>
import { mapState } from "vuex";

export default {
  data() {
    return {
      chartInstance: null,
      allData: [],
      currentPage: 1, //当前显示的页数
      totalPage: 0, //总页数
      timerId: null,
    };
  },
  created() {
    // console.log(this.$nextTick);
    //在组件创建完成后，进行回调函数的注册
    this.$socket.registerCallBack("sellerData", this.getData);
  },
  mounted() {
    //dom元素挂载完成
    this.initChart();
    // this.getData();
    //发送数据给服务器,获取数据
    this.$socket.send({
      action: "getData",
      socketType: "sellerData",
      chartName: "seller",
      value: "",
    });
    window.addEventListener("resize", this.screenAdapter);
    //在页面加载完成时,主动进行屏幕的适配
    this.screenAdapter();
  },
  destroyed() {
    clearInterval(this.timerId);
    //组件销毁时,取消监听器
    window.removeEventListener("resize", this.screenAdapter);
    //在组件销毁后，进行回调函数的移除
    this.$socket.removeCallBack("sellerData");
  },
  watch: {
    theme() {
      this.chartInstance.dispose(); //销毁当前图表
      this.initChart(); //重新初始化新主题的图表
      this.updateChart(); //更新图表
      this.screenAdapter(); //完成图表的适配
    },
  },
  computed: {
    ...mapState(["theme"]),
  },
  methods: {
    //初始化echarts
    initChart() {
      this.chartInstance = this.$echarts.init(this.$refs.seller_ref, this.theme);
      //对图表初始化的配置
      const initOption = {
        title: {
          text: "▎商家销售统计",
          left: 20,
          top: 20,
        },
        //坐标原点的位置
        grid: {
          top: "20%",
          left: "3%",
          right: "6%",
          bottom: "3%",
          containLabel: true, //包含坐标文字的距离
        },
        xAxis: {
          type: "value",
        },
        yAxis: {
          type: "category",
        },
        //工具提示栏
        tooltip: {
          trigger: "axis",
          axisPointer: {
            type: "line",
            z: 0,
            lineStyle: {
              color: "#2D3443",
              type: "solid",
            },
          },
        },
        series: [
          {
            type: "bar",
            label: {
              show: true,
              position: "right",
              textStyle: {
                color: "white",
              },
            },
            itemStyle: {
              //指明颜色渐变的方向,不同百分比下的颜色
              color: new this.$echarts.graphic.LinearGradient(0, 0, 1, 0, [
                //百分之0状态的颜色值
                {
                  offset: 0,
                  color: "#5052EE",
                },
                //百分之百状态的颜色值
                {
                  offset: 1,
                  color: "#AB6EE5",
                },
              ]),
            },
          },
        ],
      };
      this.chartInstance.setOption(initOption);
      //对图表对象进行监听
      this.chartInstance.on("mouseover", () => {
        clearInterval(this.timerId);
      });
      this.chartInstance.on("mouseout", () => {
        this.startInterval();
      });
    },
    //获取数据
    async getData(ret) {
      // const { data: ret } = await this.$http.get("/seller");
      // console.log(ret);
      this.allData = ret;
      //对数组进行排序
      this.allData.sort((a, b) => {
        return a.value - b.value;
      });
      //每五个元素显示一页
      this.totalPage =
        this.allData.length % 5 === 0
          ? this.allData.length / 5
          : this.allData.length / 5 + 1;
      this.updateChart();
      this.startInterval();
    },
    //更新图表
    updateChart() {
      const start = (this.currentPage - 1) * 5; //0
      const end = this.currentPage * 5; //5
      const showData = this.allData.slice(start, end); //包含start不包含end
      const sellerNames = showData.map((item) => {
        return item.name;
      });
      const sellerValues = showData.map((item) => {
        return item.value;
      });
      //更新图表的配置
      const dataOption = {
        yAxis: {
          data: sellerNames,
        },
        series: [
          {
            data: sellerValues,
          },
        ],
      };
      this.chartInstance.setOption(dataOption);
    },
    //开启图表切换定时器
    startInterval() {
      if (this.timerId) {
        clearInterval(this.timerId);
      }
      this.timerId = setInterval(() => {
        this.currentPage++;
        if (this.currentPage > this.totalPage) {
          this.currentPage = 1;
        }
        this.updateChart();
      }, 3000);
    },
    //当浏览器的大小发生变化时,调用该函数,完成屏幕适配
    screenAdapter() {
      //获取图表容器的宽度
      const chartWidth = this.$refs.seller_ref.offsetWidth;
      // console.log(chartWidth);
      const titleFontSize = (chartWidth / 100) * 3.2;
      //图表屏幕分辨率的配置
      const adapterOption = {
        title: {
          textStyle: {
            fontSize: titleFontSize,
          },
        },
        //工具提示栏
        tooltip: {
          axisPointer: {
            lineStyle: {
              width: titleFontSize,
            },
          },
        },
        series: [
          {
            type: "bar",
            label: {
              show: true,
              position: "right",
              textStyle: {
                color: "white",
              },
            },
            itemStyle: {
              barBorderRadius: [0, titleFontSize / 2, titleFontSize / 2, 0],
            },
          },
        ],
      };
      this.chartInstance.setOption(adapterOption);
      //手动调用图表resize
      this.chartInstance.resize();
    },
  },
};
</script>

<style>
</style>